package com.wf.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wf.domain.SysPermission;
import com.wf.domain.SysRole;
import com.wf.domain.SysUser;
import com.wf.dto.LoginDTO;
import com.wf.dto.PageDTO;
import com.wf.dto.UserPageDTO;
import com.wf.propertity.JwtProperties;
import com.wf.service.SysUserService;
import com.wf.mapper.SysUserMapper;
import com.wf.utils.JwtUtil;
import com.wf.utils.RedisUtil;
import com.wf.vo.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author mpk16
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2024-10-02 09:54:26
*/
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{


    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    JwtProperties jwtProperties;

    @Autowired
    RedisUtil redisUtil;
    @Override
    public Map<String, Object> login(LoginDTO loginDTO) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginDTO.getUsername(), loginDTO.getPassword());

        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        SysUser user = (SysUser) authenticate.getPrincipal();
        HashMap<String, Object> map = new HashMap<>();
        String userId = user.getUserId();
        map.put("userId",userId);
        String jwt = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), map);
        redisUtil.set("login:"+userId,   JSONUtil.parse(user));

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("token",jwt);
        hashMap.put("userInfo",user);
        log.info("success");
        return hashMap;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SysRole> role(String userId) {
        List<SysRole> role =  sysUserMapper.getRolesByUserId(userId);
        return role;
    }

    @Override
    @Transactional(readOnly = true)
    public List<SysPermission> permission(String userId) {
        List<SysPermission> permission =  sysUserMapper.getPermissionByRoleId(userId);
        return permission;
    }

    @Override
    public List<SysUser> getUserList(UserPageDTO userPageDTO) {

        Page<SysUser> pageDTOPage = new Page<>(userPageDTO.getPageNo(),userPageDTO.getPageSize());
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(userPageDTO.getStartDate())&&ObjectUtil.isNotEmpty(userPageDTO.getEndDate()))
        {
            queryWrapper.between(SysUser::getCreateTime,userPageDTO.getStartDate(),userPageDTO.getEndDate());
        }
        if(ObjectUtil.isNotEmpty(userPageDTO.getUsername()))
        {
            queryWrapper.like(SysUser::getUsername,userPageDTO.getUsername());
        }
        if(ObjectUtil.isNotEmpty(userPageDTO.getId()))
        {
            queryWrapper.eq(SysUser::getId,userPageDTO.getId());
        }
        List<SysUser> sysUsers = sysUserMapper.selectPage(pageDTOPage, queryWrapper).getRecords();

        return sysUsers;
    }

    @Override
    public  List<AdminVO> getAdminList(PageDTO pageDTO) {
        List<AdminVO> list  = sysUserMapper.getAdminList(pageDTO);

        return list;
    }
}




